var Koa = require('koa'),
	router = require('koa-router')(),
	views = require('koa-views');
var url = require('url');
const IO = require('koa-socket')
const io = new IO();
var app = new Koa();
io.attach(app);
app.use(views('views', {
	extension: 'ejs' /*应用ejs模板引擎*/
}))
router.get('/', async (ctx) => {
	let title = "空你几哇ejs";
	await ctx.render('index', {
		title: title
	});
})
router.get('/index1', async (ctx) => {
	await ctx.render('index1');
})
router.get('/index2', async (ctx) => {
	await ctx.render('index2');
})
app.use(router.routes()); /*启动路由*/
app.use(router.allowedMethods());
app._io.on('connection', socket => {
	console.log('建立连接了');
	var roomid = url.parse(socket.request.url, true).query.roomid; /*获取房间号/ 获取桌号*/
	socket.join(roomid);
	socket.on('addCart', function(data) {
		console.log(data);
		//socket.emit('serverEmit','我接收到增加购物车的事件了');  /*发给指定用户*/
		//app._io.emit('serverEmit','我接收到增加购物车的事件了');  /*广播*/
		//app._io.to(roomid).emit('serverEmit','我接收到增加购物车的事件了');
		socket.broadcast.to(roomid).emit('serverEmit', '我接收到增加购物车的事件了');
	})
})
app.listen(3000);
